import { defineStore } from 'pinia';
import { ref, computed } from 'vue';
import { KnowledgeBase } from '../types';
import apiClient from '../utils/api';

export const useKnowledgeBaseStore = defineStore('knowledgeBase', () => {
  const knowledgeBases = ref<KnowledgeBase[]>([]);
  const isLoading = ref(false);
  const error = ref<string | null>(null);
  const selectedKnowledgeBase = ref<KnowledgeBase | null>(null);

  // 计算属性
  const activeKnowledgeBases = computed(() => 
    knowledgeBases.value.filter(kb => kb.is_active)
  );

  // 获取知识库列表
  const fetchKnowledgeBases = async () => {
    try {
      isLoading.value = true;
      error.value = null;
      
      const data = await apiClient.getKnowledgeBases();
      knowledgeBases.value = data;
      
      return true;
    } catch (err: any) {
      error.value = err.response?.data?.detail || '获取知识库列表失败';
      return false;
    } finally {
      isLoading.value = false;
    }
  };

  // 创建知识库
  const createKnowledgeBase = async (data: { name: string; description: string }) => {
    try {
      isLoading.value = true;
      error.value = null;
      
      const newKb = await apiClient.createKnowledgeBase(data);
      knowledgeBases.value.unshift(newKb);
      
      return true;
    } catch (err: any) {
      error.value = err.response?.data?.detail || '创建知识库失败';
      return false;
    } finally {
      isLoading.value = false;
    }
  };

  // 更新知识库
  const updateKnowledgeBase = async (id: number, data: { name: string; description: string; is_active: boolean }) => {
    try {
      isLoading.value = true;
      error.value = null;
      
      const updatedKb = await apiClient.updateKnowledgeBase(id, data);
      const index = knowledgeBases.value.findIndex(kb => kb.id === id);
      if (index !== -1) {
        knowledgeBases.value[index] = updatedKb;
      }
      
      return true;
    } catch (err: any) {
      error.value = err.response?.data?.detail || '更新知识库失败';
      return false;
    } finally {
      isLoading.value = false;
    }
  };

  // 删除知识库
  const deleteKnowledgeBase = async (id: number) => {
    try {
      isLoading.value = true;
      error.value = null;
      
      await apiClient.deleteKnowledgeBase(id);
      knowledgeBases.value = knowledgeBases.value.filter(kb => kb.id !== id);
      
      if (selectedKnowledgeBase.value?.id === id) {
        selectedKnowledgeBase.value = null;
      }
      
      return true;
    } catch (err: any) {
      error.value = err.response?.data?.detail || '删除知识库失败';
      return false;
    } finally {
      isLoading.value = false;
    }
  };

  // 选择知识库
  const selectKnowledgeBase = (kb: KnowledgeBase | null) => {
    selectedKnowledgeBase.value = kb;
  };

  // 清除错误
  const clearError = () => {
    error.value = null;
  };

  return {
    knowledgeBases,
    isLoading,
    error,
    selectedKnowledgeBase,
    activeKnowledgeBases,
    fetchKnowledgeBases,
    createKnowledgeBase,
    updateKnowledgeBase,
    deleteKnowledgeBase,
    selectKnowledgeBase,
    clearError,
  };
});